********************************************************************************
*																		       *
* 							VAP								 				   *
*								     									       *
********************************************************************************

* -----> This do-file: All graphs using news data

********************************************************************************

set more off
clear all
set matsize 3000
set maxvar 10000

************************************

* Path 

if "`c(username)'"=="Juan S. Morales" | "`c(username)'"=="jmorales"{
	global PathData = "C:/Users/`c(username)'/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Data/"
	global PathFig = "C:/Users/`c(username)'/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Figures/"
	global PathTab = "C:/Users/`c(username)'/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Tables/"
	}
else { 
	global PathData = "/Users/JNG/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Data/"
	global PathFig = "/Users/JNG/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Figures/"
	global PathTab = "/Users/JNG/Dropbox/adamowicz/4_Draft/CPS_FINAL_submission/Tables/"
	} 

	use "${PathData}news_final.dta", clear


****gen date****
gen year = substr(date,1,4)
replace year = substr(date,7,4) if progov
destring year, replace
gen month = substr(date,6,2)
replace month = substr(date,4,2) if progov
destring month, replace
gen day = substr(date,9,2)
replace day = substr(date,1,2) if progov
destring day, replace
gen date2 = mdy(month,day,year)
drop if date2 == .
*one news article from april for some reason
drop if month==4

**preliminary analysis**
gen dOfWeek = mod(date2,7)
replace dOfWeek = dOfWeek - 2
replace dOfWeek = dOfWeek + 7 if dOfWeek<1

gen threat =  strpos(text, "nienawi") + strpos(text, "wrogo") + strpos(text, "pogard") + strpos(text, " odraza") + strpos(text, "obrzydzenie") + strpos(text, "agresja") + strpos(text, "hejt")  + strpos(text, "grozi")  + strpos(text, "grob")  + strpos(text, "zagroen")  + strpos(text, "niebezpieczestw")  >0
gen unity =  strpos(text, "jedno ") + strpos(text, "jednoi") + strpos(text, "solidarno") + strpos(text, "porozumienie") + strpos(text, "blisko ") + strpos(text, "bliskoi") >0
gen criticism =  strpos(text, "krytyczn") + strpos(text, "wadliw") + strpos(text, "nieodpowiedzi") + strpos(text, "oszustw") + strpos(text, "kamstw") + strpos(text, "nielegal") + strpos(text, "nieuczciw") + strpos(text, "niemoraln") + strpos(text, " mylc") + strpos(text, " bedn")  >0
gen twitter =  strpos(text, "Twitter") + strpos(text, "twitter")  >0

gen gov_mentions = strpos(text, "Sprawiedliwo") + strpos(text, "PiS") + strpos(text, "TVP") >0
gen opp_mentions = strpos(text, "Platform") + strpos(text, "opozycj") + strpos(text, "Opozycj") + strpos(text, "PO") + strpos(text, "Tusk") + strpos(text, "Schetyn") >0
gen text_l = lower(text)
replace text = text_l
drop text_l
replace text = subinstr(text, "á", "a",.) 
replace text = subinstr(text, "é", "e",.) 
replace text = subinstr(text, "í", "i",.) 
replace text = subinstr(text, "ó", "o",.) 
replace text = subinstr(text, "ú", "u",.)
replace text = subinstr(text, "ñ", "nh",.)
replace text = subinstr(text, ",", " ",.)
replace text = subinstr(text, `"""',  "", .)
replace text = subinstr(text, ".", " ",.) 
replace text = subinstr(text, "-", " ",.)
replace text = subinstr(text, "!", " ",.) 
replace text = subinstr(text, "/", " ",.)
replace text = subinstr(text, "…", " ",.)
replace text = subinstr(text, ":", " ",.)
replace text = subinstr(text, ";", " ",.)
replace text = subinstr(text, "#", "",.)
replace text = subinstr(text, "@", "",.)
forvalues i = 1(1)7 {
	replace text = subinstr(text, "  ", " ",.)
}

replace gov_mentions = strpos(text," rzd") + strpos(text,"minist") + strpos(text,"tvp") + strpos(text,"premier") + strpos(text,"morawieck") + strpos(text,"pisowsk") + strpos(text,"macierewicz") + strpos(text,"kaczysk") + strpos(text," msz ") + strpos(text,"smolesk") + strpos(text,"misiewicz") + strpos(text,"szydo") + strpos(text,"ziobro") + strpos(text,"gowin") + strpos(text,"brudzinski") + strpos(text,"drelich") + strpos(text,"sdownictw") >0
replace opp_mentions = strpos(text," opozycj") + strpos(text," lewac") + strpos(text,"sikorsk") + strpos(text,"schetyn") + strpos(text,"neuman") + strpos(text,"lis") + strpos(text,"tusk") + strpos(text,"platform") + strpos(text,"trzaskowski") + strpos(text,"lubnauer") >0

gen gg_adamowicz = strpos(text, "adamowicz")>0


gen ffd_dayssinceE_ada = 99999
gen ffd_targetdate_ada = 21562
foreach targetdate in 21562 {
	replace ffd_dayssinceE_ada = (date2-`targetdate') if (abs(date2-`targetdate')<=abs(ffd_dayssinceE_ada))
	replace ffd_targetdate_ada = `targetdate' if (abs(date2-`targetdate')<=abs(ffd_dayssinceE_ada))
}
gen event_days_ada = date2-21562

gen timeWindow0_ada = ffd_dayssinceE_ada>-3 & ffd_dayssinceE_ada<2
gen timeWindow1_ada = ffd_dayssinceE_ada>-6 & ffd_dayssinceE_ada<5
gen timeWindow2_ada = ffd_dayssinceE_ada>-11 & ffd_dayssinceE_ada<10
gen timeWindow3_ada = ffd_dayssinceE_ada>-16 & ffd_dayssinceE_ada<15
gen timeWindow4_ada = ffd_dayssinceE_ada>-21 & ffd_dayssinceE_ada<20
gen timeWindow5_ada = ffd_dayssinceE_ada>-31 & ffd_dayssinceE_ada<30

	
gen dayC_ada = date2 - 21562
gen dayC_sq_ada = dayC_ada*dayC_ada
gen dayC_cu_ada = dayC_ada*dayC_ada*dayC_ada
gen post_ada = date2>=21562
gen dayC_post_ada = dayC_ada*post_ada
gen dayC_sq_post_ada = dayC_sq_ada*post_ada
gen dayC_cu_post_ada = dayC_cu_ada*post_ada
gen dayC_opos_ada = dayC_ada*proopp
gen dayC_sq_opos_ada = dayC_sq_ada*proopp
gen dayC_cu_opos_ada = dayC_cu_ada*proopp
gen dayC_opos_post_ada = dayC_ada*proopp*post_ada
gen dayC_sq_opos_post_ada = dayC_sq_ada*proopp*post_ada
gen dayC_cu_opos_post_ada = dayC_cu_ada*proopp*post_ada
gen post_opo_ada = post_ada*proopp
gen post_gob_ada = post_ada*progov

gen event_days = date2-21562
bysort event_days proopp neutral: gen n_DOP = _n



****** FIGURES ********


**** Figure A1 ****

set matsize 5000
reg gg_adamowicz i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada &  timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada &  !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada &  neutral & n_DOP==1, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Opposition" 2 "Government" 3 "Neutral")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Adamowicz articles in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada &  proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada &  proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada &  !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada &  !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada &  !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada &  !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA1.png", width(3600) as(png) replace
drop coefL


**** Figure A15 ****

**opp mentions**
set matsize 5000
reg opp_mentions i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & neutral & n_DOP==1 & coefL>.2, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Pro-Opp TV" 2 "Pro-Gov TV" 3 "Neutral TV")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Opp mentions in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA15a.png", width(3600) as(png) replace
drop coefL

**gov mentions**
set matsize 5000
reg gov_mentions i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & neutral & n_DOP==1, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Pro-Opp TV" 2 "Pro-Gov TV" 3 "Neutral TV")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Gov mentions in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw 
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA15b.png", width(3600) as(png) replace
drop coefL



**** Figure A16 ****


**threat**
set matsize 5000
reg threat i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & neutral & n_DOP==1 & coefL>.2, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Pro-Opp TV" 2 "Pro-Gov TV" 3 "Neutral TV")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Threat mentions in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA16a.png", width(3600) as(png) replace
drop coefL


**criticism**
set matsize 5000
reg criticism i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & neutral & n_DOP==1, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Pro-Opp TV" 2 "Pro-Gov TV" 3 "Neutral TV")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Critical remarks in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw 
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA16b.png", width(3600) as(png) replace
drop coefL



**** Figure A17 ****

set matsize 5000
reg twitter i.date2##proopp  i.date2##neutral
predict coefL

twoway (scatter coefL event_days if timeWindow3_ada & proopp & n_DOP==1, color(orange*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1, msymbol(S) color(navy*0.62) msize(vsmall)) (scatter coefL event_days if timeWindow3_ada & neutral & n_DOP==1, scale(0.9) msymbol(T) color(green*0.62) msize(vsmall) legend( cols(1) order(1 "Pro-Opp TV" 2 "Pro-Gov TV" 3 "Neutral TV")) xsize(8) graphregion(fcolor(white) lcolor(white) lwidth(thin) ifcolor (white) ilcolor(white)) xtitle("Days since event") ytitle("Twitter mentions in news media (%)") aspectratio(0.5) graphregion(lwidth(small)) tline(0, lp(solid) lc(gray))) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & !post_ada, bwidth(3) lp(longdash) color(orange) msize(small)) (lpoly coefL event_days if timeWindow3_ada & proopp & n_DOP==1 & post_ada, bwidth(3) lp(longdash) color(orange) msize(small))  (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & !post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !neutral & !proopp & n_DOP==1 & post_ada, bwidth(3) lp(dash) msymbol(S) color(navy) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & !post_ada, bwidth(3) msymbol(S) color(green) msize(small)) (lpoly coefL event_days if timeWindow3_ada & !progov & !proopp & n_DOP==1 & post_ada, bwidth(3) msymbol(S) color(green) msize(small)), name(g1, replace) nodraw 
graph combine g1, col(1) scale(1.1) graphregion(color(white))
graph export "$PathFig/FigureA17.png", width(3600) as(png) replace
drop coefL


